"""
删除用户
"""
import pytest
from Config.Base import Base
from playwright.sync_api import Page, expect, Playwright
from Config.config import *
from Config.URL import *
import allure, os
from Config.Base import Base
from Config.Tool import tool

# 测试用例数据
case_data = {'module': '用户管理', 'caseid': '删除用户', 'account': 'zhangsan', 'username': '张三'}


@pytest.mark.基础管理系统
@pytest.mark.组织管理
@allure.step(case_data['caseid'])
# @pytest.mark.parametrize('case', case_data)
def test_case(playwright : Playwright) -> None:
    # 测试结果标题
    allure.dynamic.feature(case_data['module'])
    allure.dynamic.title(case_data['caseid'])
    allure.dynamic.suite(case_data['caseid'])

    SetupTeardown = Base(playwright)
    SetupTeardown.goto(url)
    SetupTeardown.login(username_admin, password_admin)
    context = SetupTeardown.context
    page = SetupTeardown.page
    page.wait_for_timeout(2000)

    page.goto(url + url_yonghuguanli)


    page.wait_for_timeout(2000)
    page.get_by_text("账号姓名查询清空").click()


    page.get_by_role("tree").get_by_text("环北部湾广西水资源配置有限公司").click()
    page.get_by_role("button", name=" 新建").click()
    page.get_by_placeholder("请输入账户").fill(case_data['account'])
    page.locator("form").filter(has_text="基础信息账户 8/200 姓名 0/200").get_by_placeholder("请输入姓名").fill(
        case_data['username'])
    page.get_by_placeholder("请选择性别").click()
    page.locator("li").filter(has_text="女").click()
    page.locator("div").filter(has_text=re.compile(r"^通用角色$")).get_by_role("textbox").click()
    page.locator("li").filter(has_text="项目管理员").click()
    page.locator("li").filter(has_text="系统管理员").click()
    page.get_by_role("button", name="确 定").click()
    # 断言
    expect(page.get_by_text('新建成功！')).to_be_visible()
    page.get_by_text("账号姓名查询清空").click()
    page.get_by_placeholder("请输入账号").fill(case_data['account'])
    page.get_by_role("textbox", name="请输入姓名").fill(case_data['username'])
    page.get_by_role("button", name=" 查询").click()
    # 获取tbody下面元素tr个数
    expect(page.locator("tbody > tr")).to_have_count(1)
    # 获取是否存在文本text
    expect(page.locator("tbody")).to_contain_text(case_data['account'])

    page.get_by_role("button", name="删除").click()
    page.get_by_role("button", name="确 定").click()
    expect(page.get_by_text('删除成功！')).to_be_visible()

    # 测试结果截图
    filename = os.path.join(test_screenshot_dir, f"{case_data['caseid']}.png")
    page.screenshot(path=filename)
    allure.attach.file(source=filename, name=case_data['caseid'],
                       attachment_type=allure.attachment_type.PNG)

    SetupTeardown.close()
